﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;

using Dynamic.Data.Model.DynamicEntities;

namespace Dynamic.Data.Model.Collections
{
    [Serializable]
    public class DynamicEntityCollection : ModelCollectionBase, IEnumerable<DynamicEntity>, IEnumerable
    {
        private Dictionary<string, DynamicEntity> _nameToDynamicEntity = new Dictionary<string, DynamicEntity>();

        public void Add(DynamicEntity entity)
        {
            this._nameToDynamicEntity[entity.Name] = entity;
        }

        public void AddRange(DynamicEntity[] entities)
        {
            foreach (DynamicEntity entity in entities)
            {
                this.Add(entity);
            }
        }

        public bool Contains(string entityName)
        {
            return this._nameToDynamicEntity.ContainsKey(entityName);
        }

        private IEnumerator<DynamicEntity> InternalGetEnumerator()
        {
            return _nameToDynamicEntity.Values.GetEnumerator();
        }

        public bool Remove(string entityName)
        {
            return this._nameToDynamicEntity.Remove(entityName);
        }

        IEnumerator<DynamicEntity> IEnumerable<DynamicEntity>.GetEnumerator()
        {
            return this.InternalGetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.InternalGetEnumerator();
        }

        public int Count
        {
            get
            {
                return this._nameToDynamicEntity.Count;
            }
        }

        public DynamicEntity this[string entityName]
        {
            get
            {
                return this._nameToDynamicEntity[entityName];
            }
            set
            {
                this._nameToDynamicEntity[entityName] = value;
            }
        }
    }
}
